package com.baoyang.job;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baoyang.entity.Container;
import com.baoyang.mapper.ContainerMapper;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 货柜定时任务
 */
@Component
public class ContainerJob {

    @Resource
    private ContainerMapper containerMapper;

//    @Scheduled(fixedRate = 5000)
    // 每天凌晨1点执行
    @Scheduled(cron = "0 0 1 * * ?")
    public void dailyTask() {
        List<Container> containerList = containerMapper.selectList(Wrappers.<Container>lambdaQuery().eq(Container::getTransportationStatus, 2)
                .in(Container::getType, 1, 2));
        if (CollectionUtils.isNotEmpty(containerList)) {
            Map<Integer, List<Container>> containerGroupByType = containerList.stream().collect(Collectors.groupingBy(Container::getType));
            for (Map.Entry<Integer, List<Container>> entry : containerGroupByType.entrySet()) {
                switch (entry.getKey()) {
                    case 1:
                        List<Container> expireContainers = containerGroupByType.get(1);
                        if (CollectionUtils.isNotEmpty(expireContainers)) {
                            for (Container container : expireContainers) {
                                // 如果截止日期等于今天的日期，则设置状态为已到期
                                if (container.getDeadlineDate().equals(LocalDate.now())) {
                                    container.setType(2);
                                    containerMapper.updateById(container);
                                } else if (container.getDeadlineDate().isBefore(LocalDate.now())) {
                                    // 如果截止日期大于今天的日期，则设置状态为已过期
                                    container.setType(3);
                                    containerMapper.updateById(container);
                                }
                            }
                        }
                        break;
                    case 2:
                        // 获取所有到期的货柜
                        List<Container> becomeDueContainers = containerGroupByType.get(2);
                        if (CollectionUtils.isNotEmpty(becomeDueContainers)) {
                            for (Container container : becomeDueContainers) {
                                // 如果截止日期大于今天的日期，则设置状态为已过期
                                if (container.getDeadlineDate().isBefore(LocalDate.now())) {
                                    container.setType(3);
                                    containerMapper.updateById(container);
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
